package org.example.util;




import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
* Cloud User Center System
*
* File: ValidationUtil.java
* Version: 0.1
* 
* ChinaNetCenter Inc.
* Copyright (C): 
*
* Description:
* 
* Action 校验类
*
* Notes:ValidationUtil.java
 * @author Administrator
*/
public class ValidationUtil 
{

	/**
	 * hasMap初始化默认值
	 */
	public static final Integer MAP_VALUE = 16;

	/**
	 * 定义IP校验
	 */
	private static Pattern patternIp = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");;

	/**
	 * 
	 * 验证值是否是空
	 * 
	 * @param object	验证值
	 * @return	验证值是否是空
	 */
	public static boolean isEmpty(Object object)
	{
		boolean isEmpty = false;
		
		if(object == null)
		{
			isEmpty = true;
		}
		//验证字符串类型
		else if(object instanceof String)
		{
			String validatedObject = (String)object;
			if(validatedObject == null || "".equals(validatedObject.trim()))
			{
				isEmpty = true;
			}
		}
		// 验证集合类型
		else if(object instanceof Collection)
		{
			Collection validatedObject = (Collection)object;
			if(validatedObject == null || validatedObject.size() == 0 )
			{
				isEmpty = true;
			}
		}
		// 验证Map类型
		else if(object instanceof Map)
		{
			Map validatedObject = (Map)object;
			if(validatedObject == null || validatedObject.size() == 0 )
			{
				isEmpty = true;
			}
		}
		// 验证日期类型
		else if(object instanceof Date)
		{
			Date validatedObject = (Date)object;
			if(validatedObject == null)
			{
				isEmpty = true;
			}
		}
		// 验证日期类型
		else if(object instanceof Timestamp)
		{
			Timestamp validatedObject = (Timestamp)object;
			if(validatedObject == null)
			{
				isEmpty = true;
			}
		}
		// 验证Set类型
		else if(object instanceof Set)
		{
			Set validatedObject = (Set)object;
			if(validatedObject == null || validatedObject.size() == 0)
			{
				isEmpty = true;
			}
		}
		
		return isEmpty;
	}
	
	/**
	 * 
	 * 验证数组是否为空
	 * 
	 * @param objects	对象数组
	 * @return
	 */
	public static boolean isEmpty(Object[] objects)
	{
		boolean isEmpty = false;
		
		if(objects == null || objects.length == 0)
		{
			isEmpty = true;
		}
		
		return isEmpty;
	}
	
	public static boolean isValidEndDate(String endDateString, int num){
		//小写的mm表示的是分钟
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date endDate=null;
		try {
			endDate=sdf.parse(endDateString);
		} catch (ParseException e) {
			return false;
		}
		Date nowDate=new Date();
	    Calendar calendar  =  new GregorianCalendar();
	    calendar.setTime(nowDate);
		//把日期往后增加一天.整数往后推,负数往前移动
	    calendar.add(Calendar.DATE,num*30);
	    Date lastDate=calendar.getTime();
	    if(endDate.after(lastDate) || endDate.before(nowDate)){
	    	return false;
	    }
		return true;
	}


	/**
	 * hashMap 初始化值
	 * @param inValue 传入初始值，默认16
	 */
	public static Integer getHashMapValue(Integer inValue){
		if(ValidationUtil.isEmpty(inValue)){
			return MAP_VALUE;
		}else{
			return (int) ((float) inValue / 0.75F + 1.0F);
		}
	}

	/**
	 * 校验IP地址
	 * @param ipAddress
	 * @return
	 */
	public static boolean isIP(String ipAddress) {
		if (ValidationUtil.isEmpty(ipAddress)){
			return false;
		}
		Matcher matcher = patternIp.matcher(ipAddress);
		return matcher.matches();
	}

	
	public static void main(String[] args) {
		 System.out.println(isValidEndDate("2017-12-20",14));
	}

	public static boolean isNotEmpty(Object object){
		return !isEmpty(object);
	}
}